package gui;

import robots.Ghost;

public class Sorting {

	/**
	 * @param args
	 */
	 public static void main(String[ ] args)
     {
		 String[] names = {"adam", "adem", "edem", "ace","e","a"};
		 String[] sorting = sortStringBubble(names);
		 for ( int k = 0;  k < 4;  k++ )
			 System.out.println(sorting[k]);
     }
	 public static String[] sortStringBubble( String  x [ ] )
	 {
		 int j;
		 boolean flag = true;  // will determine when the sort is finished
		 String temp;

		 while ( flag )
		 {
			 flag = false;
			 for ( j = 0;  j < x.length - 1;  j++ )
			 {
				 if ( x [ j ].compareToIgnoreCase( x [ j+1 ] ) > 0 )
				 {                                             // ascending sort
					 temp = x [ j ];
				 x [ j ] = x [ j+1];     // swapping
				 x [ j+1] = temp;
				 flag = true;
				 }
			 }
		 }
		 return x;
	 }
	 
	 public static Ghost[] sortGhosts(Ghost[] g){
		 String[] names = new String[g.length];
		 for (int i = 0; i < names.length; i++) {
			 if (g[i] != null)
				 names[i] = g[i].getName();
			 else
				 names[i] = "";
		}
		 names = sortStringBubble(names);
		 Ghost[] ghosts = new Ghost[g.length];
		 
		 for (int i = 0; i < names.length; i++) {
				
			 ghosts[i] = getGhostFromName(names[i],g);
			}
		 
		 return ghosts;
	 }
	 
	 private static Ghost getGhostFromName(String name, Ghost[] g){
		 for (Ghost ghost : g) {
			if ( ghost != null && ghost.getName().equals(name))
				return ghost;
		}
		 return null;
	 }
	 
}
    

      
